package controller;


import com.google.gson.Gson;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Comment;
import pojo.User;
import service.CommentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Resource
    private CommentService commentService;


    @RequestMapping("/insertComment")
    public void insertComment(@RequestParam Map<String, String> map) {
        String commentStr = map.get("commentStr");
        Gson gson = new Gson();
        Comment comment = gson.fromJson(commentStr, Comment.class);
        System.out.println("评论model" + commentStr);
        commentService.insertComment(comment);
    }

    @RequestMapping("/SelectComment")
    public void SelectComment(@RequestParam Map<String, String> map, HttpServletResponse response) {
        List<Comment> commentList = new ArrayList<Comment>();
        commentList = commentService.selectComment(map.get("selectcomment"));
        Gson gson = new Gson();
        List<Comment> commentLists = new ArrayList<Comment>();
        if (commentList.size() == 0) {
            Comment comment = new Comment();
            User user = new User();
            user.setName("欢迎");
            user.setUserid("1");
            comment.setCommentid(1);
            comment.setContent("欢迎浏览");
            comment.setmCommentator(user);
            System.out.println(comment.toString());
            commentLists.add(comment);
        } else {
            for (int i = 0; i < commentList.size(); i++) {
                Comment comment = new Comment();
                comment.setCommentid(commentList.get(i).getCommentid());
                comment.setContent(commentList.get(i).getContent());
                comment.setmCommentator(commentList.get(i).getmCommentator());
                comment.setmReceiver(commentList.get(i).getmReceiver());
                comment.setBlogid(commentList.get(i).getBlogid());
                System.out.println(comment.toString());
                commentLists.add(comment);
            }
        }
        String commentStr = new Gson().toJson(commentLists);
        System.out.println(commentStr);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(commentStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("getcommentbybeuserid")
    public void getcommentbybeuserid(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String userid = map.get("userid");
        Gson gson = new Gson();
        List<Comment> list = commentService.getcommentbybeuserid(userid);
     /*   System.out.println(list);*/
         List<Comment> list1=new ArrayList<Comment>();
        for (int i = 0; i <list.size() ; i++) {
            Comment comment=new Comment();
            comment.setCommentid(list.get(i).getCommentid());
            comment.setmCommentator(list.get(i).getmCommentator());
            comment.setmContent(list.get(i).getmContent());
            comment.setBlogid(list.get(i).getBlogid());
            list1.add(comment);

        }
        String JsonStr = new Gson().toJson(list1);
        System.out.println(JsonStr);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @CrossOrigin
    @RequestMapping("/SelectAllComment")
    public void SelectAllComment(@RequestParam Map<String, String> map, HttpServletResponse response) {
        List<Comment> commentList = new ArrayList<Comment>();
        commentList = commentService.selectAllComment();
        Gson gson = new Gson();
        List<Comment> commentLists = new ArrayList<Comment>();
        if (commentList.size() == 0) {
            Comment comment = new Comment();
            User user = new User();
            user.setName("欢迎");
            user.setUserid("1");
            comment.setCommentid(1);
            comment.setContent("欢迎浏览");
            comment.setmCommentator(user);
            System.out.println(comment.toString());
            commentLists.add(comment);
        } else {
            for (int i = 0; i < commentList.size(); i++) {
                Comment comment = new Comment();
                comment.setCommentid(commentList.get(i).getCommentid());
                comment.setContent(commentList.get(i).getContent());
                comment.setmCommentator(commentList.get(i).getmCommentator());
                comment.setmReceiver(commentList.get(i).getmReceiver());
                comment.setBlogid(commentList.get(i).getBlogid());
                System.out.println(comment.toString());
                commentLists.add(comment);
            }
        }
        String commentStr = new Gson().toJson(commentLists);
        System.out.println(commentStr);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(commentStr);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @CrossOrigin
    @RequestMapping("/deleteComment")
    public void deleteComment(int id) {
        System.out.println(id);
        commentService.delete(id);


    }


}





